home *** CD-ROM | disk | FTP | other *** search
- Path: colossus.holonet.net!russell
- From: russell@news.mdli.com (Russell Blackadar)
- Newsgroups: comp.lang.c++
- Subject: Re: Help with Bug Pleeeeease!
- Date: 29 Jan 1996 14:59:00 GMT
- Organization: HoloNet National Internet Access System: 510-704-1058/modem
- Message-ID: <4einbk$ia9@colossus.holonet.net>
- References: <tday-2801961838030001@tday.slip.netcom.com>
- NNTP-Posting-Host: jubal.mdli.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Tony Day (tday@netcom.com) wrote:
-
- : I hope some C++ wizard out there can help me debug this apparantly simple
- : program. I am teaching myself C++ with Prata╣s │C++ primer +▓ and am
- : completely stuck with one of the problems at the end of CH11.
-
- Well, I found one bug that would explain the crash:
-
- : String String::operator+(const String & st) const
- : {
- ...
- : temp1.str = new char[temp1.len+1];
- : strcpy(temp1.str,str);
- : for (int i=len; i< temp1.len; i++)
- : temp1.str[i]=st.str[i-len];
- : temp1.str[temp1.len+1]='\0';
- ^^^^^^^^^^^
- Don't forget, the array starts at 0 so the last array element is
- temp1.str[temp1.len]. You are storing into the byte AFTER the end
- of the string. Crash!
-
- Actually, you could avoid a lot of trouble if you just replace your
- for loop with another strcpy:
- strcpy(temp1.str+len, st.str); // replaces for loop
- and of course you wouldn't need the erroneous statement, either, then.
- --
- Russell Blackadar, russell@mdli.com
-